package com.fernfx.xingtan.my.ui;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.common.network.OkHttpProxy;
import com.fernfx.xingtan.my.contract.RealVerifyContract;
import com.fernfx.xingtan.my.entity.RealVerifyEntity;
import com.fernfx.xingtan.my.presenter.RealVerifyPresenter;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.RegexUtil;
import com.fernfx.xingtan.utils.SoftInputUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by admin on 2018/8/15.
 * 实名认证
 */

public class RealVerifyActivity extends BaseActivity implements View.OnClickListener, RealVerifyContract.View {
    @BindView(R.id.name_edit)
    EditText nameEdit;
    @BindView(R.id.id_Card_edit)
    EditText idCardEdit;
    @BindView(R.id.real_verify_user_bt)
    Button verifyBt;

    private RealVerifyContract.Presenter presenter;

    public static void start(Context context) {
        Intent intent = new Intent(context, RealVerifyActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_my_setting_real_verify;
    }

    @Override
    protected void initData() {
        presenter = new RealVerifyPresenter();
        presenter.init(this);
        //实名验证状态
        requestArgsMap.clear();
        presenter.request(requestArgsMap);
    }

    @Override
    protected void initView() {
        setToolbarTitle("实名认证");
    }

    @OnClick({R.id.real_verify_user_bt,R.id.real_verify_clt})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.real_verify_clt:
                SoftInputUtil.hideKeyboard(this);
                break;
            case R.id.real_verify_user_bt:
                String nameText = trimEditText(nameEdit);
                if (TextUtils.isEmpty(nameText)) {
                    ToastUtil.showCentertoast("姓名不能为空");
                    return;
                }
                String idCardText = trimEditText(idCardEdit);
                if (!RegexUtil.isCard(idCardText)) {
                    ToastUtil.showCentertoast("请输入正确的身份证号码");
                    return;
                }
                requestArgsMap.clear();
                requestArgsMap.put("username",nameText);
                requestArgsMap.put("identityNum",idCardText);
                presenter.saveAuthentication(requestArgsMap);
                break;
        }
    }

    @Override
    public void showRealVerifyContent(RealVerifyEntity realVerifyEntity) {
        int isAudit = realVerifyEntity.getObj().getIsAudit();
        if (!(isAudit == 0 || isAudit == 3)) {
            //认证了则不能做操作
            nameEdit.setEnabled(false);
            nameEdit.setText(OtherUtil.checkEmptyDefault(realVerifyEntity.getObj().getUsername()));
            idCardEdit.setEnabled(false);
            idCardEdit.setText(OtherUtil.checkEmptyDefault(realVerifyEntity.getObj().getIdentityNum()));
            verifyBt.setClickable(false);
            //提示用户
            ToastUtil.showCentertoast("已认证,或已提交认证待审核");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != presenter) {
            presenter.detachView();
            presenter = null;
        }
    }

    @Override
    public BaseActivity getActivity() {
        return this;
    }

}
